<?php

namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Http\Controllers\Api\v1\Astro\DataController;

use App\Exceptions\ApiException;
use App\Models\Api\Recode;
use Illuminate\Http\Request;


class SolarreturnController extends BaseController
{

    /**
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get(Request $request)
    {
        $datetime = htmlspecialchars($request->input('datetime'));

        $time = strtotime($datetime);
        $timeinfo = [
            'year'=>date('Y',$time),
            'month'=>date('m',$time),
            'day'=>date('d',$time),
            'hour'=>date('H',$time),
            'minute'=>date('i',$time),
        ];
        $timeinfo['timezone'] = intval($request->input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'],$timeinfo['minute'],0,
            $timeinfo['month'],$timeinfo['day'],$timeinfo['year']) ? 1 : 0;

        $Data = new DataController();

        $map = ['id' => $request->input('id', 0), 'uid' => $request->uid];

        $userinfo = (new Recode())->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo);

        $longitude = ($userinfo['long_deg'] + $userinfo['long_min'] / 60 ) * $userinfo['ew'];
        $latitude = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60 ) * $userinfo['ns'];

        $timestamp2 = $Data->deal_time($timeinfo);

        $birth_JD = gregoriantojd(date('m',$timestamp1), date('d',$timestamp1), date('Y',$timestamp1)) + ((date('H',$timestamp1) + (date('i',$timestamp1) / 60)) / 24);
        $start_JD = gregoriantojd(date('m',$timestamp2), date('d',$timestamp2), date('Y',$timestamp2)) + ((date('H',$timestamp2) + (date('i',$timestamp2) / 60)) / 24);
        $days_alive = $start_JD - $birth_JD; // 总天数

        $year_ago = floor($days_alive / 365.2422);// 365.2422  365.2421990  365.25

        $now_before = strtotime("+$year_ago year",$timestamp1);
        $now_after = strtotime("+1 day",$now_before);

        $utdatenow = strftime("%d.%m.%Y", $timestamp1);
        $utnow = strftime("%H:%M:%S", $timestamp1);

        $utdatenow1 = strftime("%d.%m.%Y", $now_before);
        $utnow1 = strftime("%H:%M:%S", $now_before);

        $utdatenow2 = strftime("%d.%m.%Y", $now_after);
        $utnow2 = strftime("%H:%M:%S", $now_after);

        $Set = new AstrosetController();

        $astro_set = $Set->get(true,'solar_return');// 内部调用
        $h_sys = $astro_set['value']['h_sys'];

        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $options1 = ['utdatenow' => $utdatenow, 'utnow' => $utnow, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];
        $options2 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];
        $options3 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];

        $out1 = $Data->execData($options1);
        $out2 = $Data->execData($options2);
        $out3 = $Data->execData($options3);

        // 出生生日那一天太阳位置
        $longtitude1 = number_format(explode(',',$out1[0])[0] * 1,3);
        // 当年生日前一天的太阳位置
        $longtitude2 = number_format(explode(',',$out2[0])[0] * 1,3);
        // 当年生日后一天太阳位置
        $longtitude3 = number_format(explode(',',$out3[0])[0] * 1,3);

        $diff_longtitude = round(abs($longtitude3 - $longtitude2),6);//度数差

        $diff_minutes = 24 * 60 * 60;//精确到秒

        $diff_influence = $diff_longtitude / $diff_minutes;// 每秒钟走的度数

        // 需要多少时间  回到需要的度数
        $second = floor(abs($longtitude1 - $longtitude2) / $diff_influence);// 总秒钟数

        if ($longtitude1 < $longtitude2) $now_before -= $second;

        else $now_before += $second;

        $now_before = $now_before - ($now_before % 60) ;// 只精确到分钟

        $utdatenow_test = strftime("%d.%m.%Y", $now_before);
        $utnow_test  = strftime("%H:%M:%S", $now_before);

        $options = ['utdatenow' => $utdatenow_test, 'utnow' => $utnow_test, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];

        $out = $Data->execData($options);

        $original_data = $Data->getOriginalData($out,$h_sys);
        $Data->array_remove($original_data,$Data->planets_display);

        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1']);
        $phase = $Data->planetPkPlanet($original_data, $original_data, true);
        foreach ($phase['planets'] as $k => $d)
        {
            $details['planets'][$k]['phase'] = $d['phase'];
            if(empty($d['phase'])) $details['planets'][$k]['phase'] = null;
        }
        $details['phase'] = $phase['phase'];

        $details['return_time'] = date("Y-m-d H:i",$now_before + $userinfo['timezone'] * 3600);

        $details['planets'] = (object)$details['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        return response()->json(['code'=>200,'data'=>$details,'msg'=>'Success.']);
    }
}
